﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using ProgNetComponents.Classes;
using System.IO;

namespace BuildHelper.Work.Template
{
    public partial class GuiTemplateForm : BaseTemplateForm
    {
        public GuiTemplateForm()
        {
            InitializeComponent();
        }
        public GuiTemplateForm(string template)
            : base(template)
        {
            InitializeComponent();
        }
        private void lueBrancz_EditValueChanged(object sender, EventArgs e)
        {
            ReadBranch(lueBrancz.EditValue.AsString());
        }
        private void ReadBranch(string brancz)
        {
            changeDetectorProvider1.SetModified();
            string sVs, sKonfig;
            Work.GUI.BudowanieGuiTools.ReadBranch(brancz, guiDataSet1, out sVs, out sKonfig);
        }

        private void gvStm_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow r = gvStm.GetDataRow(e.FocusedRowHandle);
            string stm = "";
            if (r != null)
            {
                stm = r["STM"].AsString();
            }
            SetCurrentStm(stm);
        }
        private void SetCurrentStm(string stm)
        {
            gcSTM.Text = stm;
            btnZaznaczWszystkoStm.Enabled =
            btnOdznaczWszystkoSTM.Enabled = stm.IsNotNull();
        }
        #region zaznacz/odznacz
        private void ZaznaczOdznacz(string stm, bool zaznacz)
        {
            DataRow[] rows = guiDataSet1.PROJEKTY.Select("STM = " + stm.EscapeSql());
            foreach (DataRow r in rows)
            {
                changeDetectorProvider1.CheckChanges(r, r["BUDUJ"], zaznacz);
                r["BUDUJ"] = zaznacz;
            }
        }

        private void btnZaznaczWszystko_Click(object sender, EventArgs e)
        {
            foreach (DataRow r in guiDataSet1.APPS.Rows)
            {
                changeDetectorProvider1.CheckChanges(r, r["BUDUJ"], true);
                r["BUDUJ"] = true;

                ZaznaczOdznacz(r["STM"].AsString(), true);
            }
        }

        private void btnOdznaczWszystko_Click(object sender, EventArgs e)
        {
            foreach (DataRow r in guiDataSet1.APPS.Rows)
            {
                changeDetectorProvider1.CheckChanges(r, r["BUDUJ"], false);
                r["BUDUJ"] = false;
                ZaznaczOdznacz(r["STM"].AsString(), false);
            }
        }

        private void btnZaznaczWszystkoStm_Click(object sender, EventArgs e)
        {
            DataRow r = gvStm.GetFocusedDataRow();
            if (r != null)
                ZaznaczOdznacz(r["STM"].AsString(), true);
        }

        private void btnOdznaczWszystkoSTM_Click(object sender, EventArgs e)
        {
            DataRow r = gvStm.GetFocusedDataRow();
            if (r != null)
                ZaznaczOdznacz(r["STM"].AsString(), false);
        }

        #endregion
    }
}
